<?php
Class XML{
	var $Content="";
	var $RootNode="";
	var $ParentNode="";
	var $CRLF="\r\n";
	var $End="";

	Function XML($Version="1.0",$Encoding="utf-8"){
		$this->Content.="<?xml version=\"{$Version}\" encoding=\"{$Encoding}\"?>{$this->CRLF}";
	}

	Function CreateNode($NodeName="root",$Attribute=""){
		$NodeName=$this->Filter($NodeName);
		$this->RootNode=$NodeName;
		$Attribute=$this->ParseAttribute($Attribute);
		return $this->Content.="<{$NodeName}{$Attribute}>{$this->CRLF}";
	}

	Function AppendNode($NodeName,$Attribute,$Data="",$CDate=true){
		$NodeName=$this->Filter($NodeName);
		if(empty($Data)){
			if(!empty($this->ParentNode)){
				$this->Content.="</{$this->ParentNode}>{$this->CRLF}";
			}
			$this->ParentNode=$NodeName;
			$Attribute=$this->ParseAttribute($Attribute);
			return $this->Content.="<{$NodeName}{$Attribute}>{$this->CRLF}";
		}else{
			$Attribute=$this->ParseAttribute($Attribute);
			return $this->Content.=$CDate?"<{$NodeName}{$Attribute}>{$this->CRLF}<![CDATA[{$Data}]]>{$this->CRLF}</{$NodeName}>{$this->CRLF}":"<{$NodeName}{$Attribute}>{$Data}</{$NodeName}>{$this->CRLF}";
		}
	}

	Function End(){
		if($this->End){
			return $this->Content;
		}else{
			$this->End=true;
			return $this->Content=$this->ParentNode==""?$this->Content."</{$this->RootNode}>":$this->Content."</{$this->ParentNode}>{$this->CRLF}</{$this->RootNode}>";
		}
	}

	Function Display(){
		ob_start();
		header("Content-type: text/xml");
		echo $this->End();
		ob_end_flush();
	}

	Function Save($Filename){
		if(!$Handle=fopen($Filename,'wb+')){
			$this->Error("Couldn't Write File,Make Sure Your Access");
		}
		flock($Handle,LOCK_EX);
		fwrite($Handle,$this->End());
		return fclose($Handle);
	}

	Function Error($ErrorStr='',$ErrorNo='',$Stop=true){
		exit($ErrorStr);
	}

	Function ParseAttribute($Argv){
		$Attribute='';
		if(is_array($Argv)){
			foreach($Argv as $Key=>$Value){
				$Value=$this->Filter($Value);
				$Attribute.=" $Key=\"$Value\"";
			}
		}
		return $Attribute;
	}

	Function Filter($Argv){
		$Argv=trim($Argv);
		$Search=array("<",">","\"");
		$Replace=array("","","'");
		return str_replace($Search,$Replace,$Argv);
	}
}
?>